import Vue from 'vue'
import store from '@/store'
import '@/directive/index'

/** 注册全局指令 **/

// 是否拥有某角色
Vue.directive('has-role', {
  inserted: function(el, binding) {
    if (!Vue.prototype.$_has_role(binding.value)) {
      el.parentNode.removeChild(el)
    }
  }
})

Vue.prototype.$_has_role = function(role) {
  let has = false
  for (let i = 0; i < store.getters.roleList.length; i++) {
    if (store.getters.roleList[i].code === role) {
      has = true
      break
    }
  }
  return has
}

// // 是否拥有某权限
Vue.directive('has-perm', {
  inserted: function(el, binding) {
    if (!Vue.prototype.$_has_perm(binding.value)) {
      el.parentNode.removeChild(el)
    }
  }
})

Vue.prototype.$_has_perm = function(perm) {
  let has = false
  for (let i = 0; i < store.getters.permissionList.length; i++) {
    if (store.getters.permissionList[i].code === perm) {
      has = true
      break
    }
  }
  return has
}
